
<template>
    <div class="">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
        <el-breadcrumb-item>文件管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 内容区域 -->
    <el-card>
        <el-form :model="queryInfo" ref="queryForm"  size="small" :inline="true" label-width="100px">
        <el-form-item label="文件储存位置" prop="fileLocation">
            <!-- 1:阿里云，2:腾讯云，3:minio，4:本地 -->
            <el-select v-model="queryInfo.fileLocation" placeholder="请选择文件储存位置" clearable>
                <el-option label="阿里云" value="1"></el-option>
                <el-option label="腾讯云" value="2"></el-option>
                <el-option label="minio" value="3"></el-option>
                <el-option label="本地" value="4"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="文件名称" prop="fileOriginName">
            <el-input v-model="queryInfo.fileOriginName" placeholder="请输入文件名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery" type="primary" icon="el-icon-search" size="mini">搜索</el-button>
          <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 新增按钮 -->
      <el-row :gutter="10" style="marginBottom:20px">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleFile">上传文件</el-button>
        </el-col>
        <!-- 红色删除按钮 -->
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="handleDelAll">删除</el-button>
        </el-col>
      </el-row>
      <!-- 列表区域 -->
      <el-table :data="userList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="fileOriginName" label="文件名称"  align="center"> 
            <!-- 文件名称 只保持30个字符串剩下的用省略号 -->
            <template slot-scope="scope">
                <el-tooltip class="item" effect="dark" :content="scope.row.fileOriginName" placement="top">
                    <div class="item" v-if="scope.row.fileOriginName.length>15">{{scope.row.fileOriginName.substring(0, 15) + '...'}}</div>
                    <div class="item" v-else>{{scope.row.fileOriginName}}</div>
                </el-tooltip>
            </template>    
        </el-table-column>
        <el-table-column prop="smallPicture" label="缩略图"  align="center" style="width: 40px;">
            <!-- 如果fileSuffix为常见的图片格式，给scope.row.smallPicture 加上base64的图片格式前缀，如果不为图片格式则展示一个icon图标 -->
            <template slot-scope="scope">
                <el-image v-if="scope.row.fileSuffix === 'jpg' || 
                scope.row.fileSuffix === 'png' ||
                scope.row.fileSuffix === 'jpeg' ||
                scope.row.fileSuffix === 'gif'" 
                :src="'data:image/png;base64,' + scope.row.smallPicture" style="width: 40px; height: 40px; border-radius: 50%;"
                ></el-image>
                <i v-else class="el-icon-folder"></i>
            </template>
        </el-table-column>
        <el-table-column prop="fileSizeKb" label="文件大小"  align="center"> 
            <!-- 加个kb -->
            <template slot-scope="scope">
                {{scope.row.fileSizeKb}}kb
            </template>
        </el-table-column>
        <el-table-column prop="fileSuffix" label="文件后缀"  align="center"> </el-table-column>
        <el-table-column prop="fileLocation" label="储存引擎"  align="center"> 
            <!-- 文件存储位置（1:阿里云，2:腾讯云，3:minio，4:本地） -->
            <template slot-scope="scope">
                <span v-if="scope.row.fileLocation === 1">阿里云</span>
                <span v-if="scope.row.fileLocation === 2">腾讯云</span>
                <span v-if="scope.row.fileLocation === 3">minio</span>
                <span v-if="scope.row.fileLocation === 4">本地</span>
            </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="220" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <!-- 详情按钮 -->
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)">详情</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="hanldeDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>         
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!-- 弹出框 发送文件-->
    <el-dialog :title="upload.title" width="600px"  :visible.sync="dialogVisible"  style="text-align: center;"> 
        <el-upload
            class="upload-demo"
            drag
            action="#"
            :http-request="customUpload"
            multiple>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            </el-upload>
    </el-dialog>
    <!-- 详情抽屉 -->
    <el-drawer
        title="详情"
        :visible.sync="drawer"
        direction="rtl"
        :before-close="handleClose">
        <!-- 写一个左右布局的框 -->
        <div class="DtealiBox">
            <ul>
                <li>
                    <span>文件名称：</span>
                    <span>{{detailData.fileOriginName}}</span>
                </li>
                <li>
                    <span>文件引擎：</span>
                    <span v-if="detailData.fileLocation === 1">阿里云</span>
                    <span v-if="detailData.fileLocation === 2">腾讯云</span>
                    <span v-if="detailData.fileLocation === 3">minio</span>
                    <span v-if="detailData.fileLocation === 4">本地</span>
                    <!-- <span>{{detailData.fileLocation}}</span> -->
                </li>
                <li>
                    <span>储存桶</span>
                    <span>{{detailData.fileObjectName}}</span>
                </li>
                <li>
                    <span>上传时间</span>
                    <span>{{detailData.createTime}}</span>
                </li>
                <li>
                    <span>大小</span>
                    <span>{{detailData.fileSizeKb}}KB</span>
                </li>
            </ul>
        </div>
        <div class="filePath">
            <div>储存路径</div>
            <div>{{ detailData.filePath}}</div>
        </div>
        <div class="url">
            <div>文件地址</div>
            <div>
            <!-- 用Popover -->
            <el-popover
                style="margin: 10px 0 0 10px;"
                placement="top-start"
                title="文件地址"
                width="300"
                trigger="hover"
                :content="detailData.url">                
                <el-button type="text" slot="reference" @click="hanldeSkip">{{ shortenedAddress }}</el-button>
            </el-popover>
            
        </div>
        </div>

        </el-drawer>
    </div>

</template>

<script>
export default {
  name: 'fileManagement',
  data () {
    return {
        userList: [],
        total:0,
        queryInfo: {
            // 当前的页数
            currentPage: 1,
            // 当前每页显示多少条数据
            pageSize: 10,
        },
      upload: {
            // 弹出层标题（用户导入）
            title: '',
          },
      // 弹出层显示隐藏
      dialogVisible: false,
      //抽屉显示隐藏
      drawer: false,
      detailData: {
        url:''
      },
      addForm: {},
    //选中的id
    ids: [],
    //弹出框状态
    }
},
created() {
    this.listFun()
},  
methods:{
    // 搜索按钮操作
    handleQuery() {
      this.queryInfo.currentPage = 1
      this.listFun()
    },
    // 重置按钮操作
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },
    // 监听 pageSize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pageSize = newSize
      this.listFun()
    },
    // 监听页码值改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.currentPage = newPage
      this.listFun()
    },
    async listFun(){
        const params = this.queryInfo
        const { data: res } = await this.$http.post('/sys-file/select', params)
        if (res.code === 20000) {
          this.userList = res.data.records
          this.total = parseInt(res.data.total)
        } else {
          this.$message.error(res.msg)
        }
    },
    handleFile(){
        this.upload.title = '上传文件'
        this.dialogVisible = true
    },
    handleDetail(row){
        this.drawer = true
        ///sys-file/detail get请求
        const url = `/sys-file/detail?fileId=${row.fileId}`;
        this.$http.get(url).then(res => {
            if (res.data.code === 20000) {
                this.detailData = res.data.data
            } else {
                this.$message.error(res.data.msg)
            }
        })
    },
    hanldeDel(row){
        this.$confirm('确认删除吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }).then(async () => {
            const url = `/sys-file/delete?fileIds=${row.fileId}`;
            const { data: res } = await this.$http.delete(url)
            if (res.code === 20000) {
                this.$message.success('删除成功')
                this.listFun()
            } else {
                this.$message.error(res.msg)
            }
        })
    }, 
    handleDelAll(){
        if (this.ids.length === 0) {
            this.$message.error('请选择要删除的数据')
            return
        }
        this.$confirm('确认删除吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }).then(async () => {
            const url = `/sys-file/delete?fileIds=${this.ids.join(',')}`;
            const { data: res } = await this.$http.delete(url)
            if (res.code === 20000) {
                this.$message.success('删除成功')
                this.listFun()
            } else {
                this.$message.error(res.msg)
            }
        })
    },
    handleSelectionChange(selection){
        console.log(selection);
        this.ids = []
        selection.forEach(item => {
          this.ids.push(item.fileId)
        })
    },
    async customUpload(file) {
            const { data: res } = await this.$http.put('/sys-file/upload', {file:file.file},{
                headers: {
                 'content-type': "multipart/form-data" 
                }
            })
            if (res.data) {
                this.$message.success('上传成功')
                this.listFun()
            } else {
                this.$message.error(res.msg)
            }
        },
    //抽屉关闭
    handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      hanldeSkip(){
        //跳转到Detail.url
        window.open(this.detailData.url, '_blank');
      }
},
computed: {
    // 计算属性，用于截取前30个字符
    shortenedAddress() {
      if (this.detailData.url.length > 40) {
        return this.detailData.url.substring(0, 40) + '...';
      } else {
        return this.detailData.url;
      }
    },
    
  },
}
</script>

<style scoped lang="less">
.DtealiBox{
    width: 450px;
    margin-left: 5px;
    ul{
        padding-left:0px ;
        //关闭ul样式
        list-style :none;
        li{
            padding:10px 0 10px 0;
            border: 1px solid #eee;
            display: flex;
            :nth-child(1){
                display: inline-block;
                width: 100px;
                padding: 5px 0px 5px 15px;
            }
            :nth-child(2){
                display: inline-block;
                padding: 5px 0px 5px 15px;
            }
        }
    }
}
.filePath{
    width: 450px;
    display: flex;
    flex-wrap: wrap;
    margin: 10px 5px 10px 5px;
}
.url{
    width: 450px;
    height: auto;
    margin: 10px 5px 10px 5px;
    .langurl{
        width: 450px;
        overflow: hidden;
    }
}

</style>